//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FailureToPay.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Experimental/Credit/DefaultType.h>
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CFailureToPay::CFailureToPay (Cephei::QL::Times::IPeriod^ grace, Microsoft::FSharp::Core::FSharpOption<Double>^ amount) : CDefaultType(CFailureToPay::typeid)
{
    CPeriod^ _Cgrace;
    try
    {
#ifdef HANDLE
        _phFailureToPay = NULL;
#endif
        _Cgrace = safe_cast<CPeriod^> (grace);
        _Cgrace->Lock();
        QuantLib::Period& _grace = static_cast<QuantLib::Period&> (_Cgrace->GetReference ()); 
        QuantLib::Real _amount = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (amount) ? (QuantLib::Real)ValueHelper::Convert (amount->Value) : 1.e+6); //4
        _ppFailureToPay = new boost::shared_ptr<QuantLib::FailureToPay> (new QuantLib::FailureToPay ( _grace,  _amount ));
        SetDefaultType (boost::dynamic_pointer_cast<QuantLib::DefaultType> (*_ppFailureToPay));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgrace != nullptr) _Cgrace->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CFailureToPay::CFailureToPay (boost::shared_ptr<QuantLib::FailureToPay>& childNative, Object^ owner) : CDefaultType(CFailureToPay::typeid)
{
#ifdef HANDLE
	_phFailureToPay = NULL;
#endif
	_ppFailureToPay = &childNative;
    _ppDefaultType = new boost::shared_ptr<QuantLib::DefaultType> (boost::dynamic_pointer_cast<QuantLib::DefaultType> (*_ppFailureToPay));
}
Cephei::QL::Experimental::Credit::CFailureToPay::CFailureToPay (QuantLib::FailureToPay& childNative, Object^ owner) : CDefaultType(CFailureToPay::typeid)
{
#ifdef HANDLE
	_phFailureToPay = NULL;
#endif
	_ppFailureToPay = new boost::shared_ptr<QuantLib::FailureToPay> (&childNative);
    _ppDefaultType = new boost::shared_ptr<QuantLib::DefaultType> (boost::dynamic_pointer_cast<QuantLib::DefaultType> (*_ppFailureToPay));
    _FailureToPayOwner = owner;
    _DefaultTypeOwner = owner;
}

Cephei::QL::Experimental::Credit::CFailureToPay::CFailureToPay (CFailureToPay^ copy) : CDefaultType(CFailureToPay::typeid)
{
#ifdef HANDLE
	_phFailureToPay = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFailureToPay = new boost::shared_ptr<QuantLib::FailureToPay> (copy->GetShared());
        _ppDefaultType = new boost::shared_ptr<QuantLib::DefaultType> (boost::dynamic_pointer_cast<QuantLib::DefaultType> (*_ppFailureToPay));
    }
}
Cephei::QL::Experimental::Credit::CFailureToPay::CFailureToPay (PLATFORM::Type^ t) : CDefaultType(CFailureToPay::typeid)
{
#ifdef HANDLE
	_phFailureToPay = NULL;
#endif
	if (!t->IsSubclassOf(CFailureToPay::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CFailureToPay::CFailureToPay (QuantLib::Handle<QuantLib::FailureToPay>& childNative, Object^ owner)  : CDefaultType(CFailureToPay::typeid)
{
	_phFailureToPay = &childNative;
	_ppFailureToPay = &static_cast<boost::shared_ptr<QuantLib::FailureToPay>>(childNative.currentLink());
    _ppDefaultType = new boost::shared_ptr<QuantLib::DefaultType> (boost::dynamic_pointer_cast<QuantLib::DefaultType> (*_ppFailureToPay));
    _FailureToPayOwner = owner;
}
Cephei::QL::Experimental::Credit::CFailureToPay::CFailureToPay (QuantLib::Handle<QuantLib::FailureToPay> childNative)  : CDefaultType(CFailureToPay::typeid)
{
	_phFailureToPay = &childNative;
	_ppFailureToPay = &static_cast<boost::shared_ptr<QuantLib::FailureToPay>>(childNative.currentLink());
    _ppDefaultType = new boost::shared_ptr<QuantLib::DefaultType> (boost::dynamic_pointer_cast<QuantLib::DefaultType> (*_ppFailureToPay));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CFailureToPay::CFailureToPay (QuantLib::FailureToPay childNative)  : CDefaultType(CFailureToPay::typeid)
{
#ifdef HANDLE
	_phFailureToPay = NULL;
#endif
	_ppFailureToPay = new boost::shared_ptr<QuantLib::FailureToPay> (new QuantLib::FailureToPay (childNative));
    _ppDefaultType = new boost::shared_ptr<QuantLib::DefaultType> (boost::dynamic_pointer_cast<QuantLib::DefaultType> (*_ppFailureToPay));
}
#endif

Cephei::QL::Experimental::Credit::CFailureToPay::~CFailureToPay ()
{
    if (_ppFailureToPay != NULL)
    {
	    delete _ppFailureToPay;
        _ppFailureToPay = NULL;
    }
}
Cephei::QL::Experimental::Credit::CFailureToPay::!CFailureToPay ()
{
    if (_ppFailureToPay != NULL)
    {
	    delete _ppFailureToPay;
    }
}
QuantLib::FailureToPay& Cephei::QL::Experimental::Credit::CFailureToPay::GetReference ()
{
    if (_ppFailureToPay == NULL) throw REFNEW NativeNullException ();
	return **_ppFailureToPay;
}
boost::shared_ptr<QuantLib::FailureToPay>& Cephei::QL::Experimental::Credit::CFailureToPay::GetShared ()
{
    if (_ppFailureToPay == NULL) throw REFNEW NativeNullException ();
	return *_ppFailureToPay;
}
QuantLib::FailureToPay* Cephei::QL::Experimental::Credit::CFailureToPay::GetPointer ()
{
    if (_ppFailureToPay == NULL) throw REFNEW NativeNullException ();
	return &**_ppFailureToPay;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FailureToPay>& Cephei::QL::Experimental::Credit::CFailureToPay::GetHandle ()
{
	if (_phFailureToPay == NULL)
	{
		_phFailureToPay = new Handle<QuantLib::FailureToPay> (*_ppFailureToPay);
	}
	return *_phFailureToPay;
}
#endif
bool Cephei::QL::Experimental::Credit::CFailureToPay::HasNative () 
{
	return (_ppFailureToPay != NULL);
}

Double Cephei::QL::Experimental::Credit::CFailureToPay::AmountRequired::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFailureToPay)->amountRequired ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Experimental::Credit::CFailureToPay::GracePeriod::get ()
{
    try
    {
    	QuantLib::Period& _rv = (QuantLib::Period&)(*_ppFailureToPay)->gracePeriod ( );   
        Cephei::QL::Times::CPeriod^ _nrv = REFNEW Cephei::QL::Times::CPeriod (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IFailureToPay^ Cephei::QL::Experimental::Credit::CFailureToPay_Factory::Create (Cephei::QL::Times::IPeriod^ grace, Microsoft::FSharp::Core::FSharpOption<Double>^ amount)
{
    return REFNEW CFailureToPay ( grace,  amount);
}
